<script setup>
import { ref } from "vue";
import { formatTime } from "@/utils/format";
import { Edit, Delete } from "@element-plus/icons-vue";
import { getAnncmtList, deleteAnncmt } from "@/api/anncmt";

// 控制加载
const loading = ref(false);

const drawDet = ref(null);
const drawAdd = ref(null);

// 记录总数
const total = ref(0);

// 存储请求数据
const requestData = ref({
  pagenum: 1,
  pagesize: 2,
  isTop: null,
});
// 存储公告数据
const channel = ref([
  {
    id: 101,
    title: "关于社区阅览室国庆假期开放调整的通知",
    publishTime: Date.now(),
    isTop: true,
    readCount: 328,
  },
]);
// 获取公告数据
const getAnncmtListData = async () => {
  // 开始加载
  loading.value = true;
  const res = await getAnncmtList(requestData.value);
  channel.value = res.data.data;
  total.value = res.data.total;

  // 结束加载
  loading.value = false;
};
getAnncmtListData();
// 添加公告
const onAddChannel = () => {
  drawAdd.value.handleOpen({});
};

// 编辑
const onEdit = (row) => {
  drawAdd.value.handleOpen(row);
};

// 删除
const onDelete = async (row) => {
  const res = await deleteAnncmt(row.id);
  ElMessage.success("删除成功");
  getAnncmtListData();
};

// 分页
// 每页条数变化时的回调
const handleSizeChange = (val) => {
  //console.log(val)
  requestData.value.pagesize = val;
  getAnncmtListData();
};
// 当前页码变化时的回调
const handleCurrentChange = (val) => {
  requestData.value.pagenum = val;
  getAnncmtListData();
};

// 置顶排序
const handleTopSort = (column) => {
  console.log(column);

  // column.order 表示排序状态：'ascending'（升序）、'descending'（降序）、null（取消排序）
  if (column.order === "descending") {
    // 降序：置顶数据排在前面
    channel.value.sort((a, b) => {
      return a.isTop - b.isTop;
    });
  } else if (column.order === "ascending") {
    // 升序：不置顶数据排在前面
    channel.value.sort((a, b) => {
      return b.isTop - a.isTop;
    });
  } else {
    // 取消排序：恢复原始数据（重新从接口获取）
    getAnncmtListData();
  }
};

// 公告详情
const handleDetails = (row) => {
  drawDet.value.handleOpen(row.id);
};
// 接收子组件传过来的值 刷新列表
const success = () => {
  getAnncmtListData();
};
</script>

<template>
  <Card title="公告管理">
    <!-- 具名插槽--按钮 -->
    <template #extra>
      <!-- 修正方法名 -->
      <el-button type="primary" @click="onAddChannel">添加公告</el-button>
    </template>
    <!-- 默认插槽--表格 -->
    <!-- 修正根组件为el-table -->
    <el-table
      :data="channel"
      style="width: 100%"
      v-loading="loading"
      @sort-change="handleTopSort"
    >
      <el-table-column label="序列" width="100" type="index" />
      <el-table-column label="标题" width="300">
        <template #default="{ row }">
          <el-link underline="never" @click="handleDetails(row)">{{
            row.title
          }}</el-link>
        </template>
      </el-table-column>
      <!-- 移除错误的prop绑定 -->
      <el-table-column label="发布时间" width="200">
        <template #default="{ row }">
          {{ formatTime(row.publishTime) }}
        </template>
      </el-table-column>
      <el-table-column label="置顶" sortable="custom">
        <template #default="{ row }">
          <el-tag :type="row.isTop ? 'success' : 'info'">
            {{ row.isTop ? "置顶" : "不置顶" }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="readCount" label="阅读人数" />
      <el-table-column label="操作" width="130">
        <template #default="{ row }">
          <el-button
            :icon="Edit"
            circle
            plain
            type="primary"
            @click="onEdit(row)"
          >
          </el-button>
          <el-button
            :icon="Delete"
            circle
            plain
            type="danger"
            @click="onDelete(row)"
          ></el-button>
        </template>
      </el-table-column>
      <!-- 空数据时 -->
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      v-model:current-page="requestData.pagenum"
      v-model:page-size="requestData.pagesize"
      :page-sizes="[2, 3, 4, 5, 10]"
      background
      layout="jumper,total, sizes, prev, pager, next"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      style="margin-top: 50px; justify-content: flex-end"
    />
  </Card>
  <!-- 预览抽屉 -->
  <DrawerDet ref="drawDet"></DrawerDet>
  <!-- 添加公告编辑 抽屉 -->
  <DrawerAdd ref="drawAdd" @success="success"></DrawerAdd>
</template>

<style scoped>
/* 样式 */
</style>
